<%@page import="com.mde.util.Validator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page import="java.sql.*" %>
<%@ page 
		language="java" 
		contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
<%@ page errorPage="error.jsp" %>    

<!DOCTYPE html>
<html>
	<title>Routes adding page</title>
	<head>
	 	<link rel="stylesheet" href="../styles.css">
		<script src="http://api-maps.yandex.ru/2.0-stable/?load=package.standard&lang=ru-RU" 
				type="text/javascript"></script>
		<script type="text/javascript" src="../js/addRoutesJs.js"></script>
	</head>
<body id="route-add-body">
	<div id="map" style="width:800px; height:600px"></div>	
	<div id="route-add-content">
<%!	
	Statement stmt;
	Connection con;
	String url = Validator.dbURL;
	String query = "SELECT id, latitude, longitude, type, name FROM Points;";
%>	
<%
	Class.forName("com.mysql.jdbc.Driver");
	con = DriverManager.getConnection(url, Validator.dbUsername, Validator.dbUserpass); 
	stmt = con.createStatement();
	
	ResultSet rs = stmt.executeQuery(query);
%>
	<table class="hidden">
		<thead>
			<tr>
				<td>id</td>
				<td>name</td>
				<td>type</td>
				<td>latitude</td>
				<td>longitude</td>
			</tr>
		</thead>
		<tbody>

<%
	int count = 0;
	while(rs.next()){
		++count;
		int id  = rs.getInt("id");
		String pointName = rs.getString("name");
		String pointType = rs.getString("type");
		double latitude = rs.getDouble("latitude");
		double longitude = rs.getDouble("longitude");
%>		
			<tr>
				<td id="<%= id %>id"><%= id %></td>
				<td id="<%= id %>name"><%= pointName %></td>
				<td><%= pointType %></td>
				<td id="<%= id %>lat"><%= latitude %></td>
				<td id="<%= id %>lon"><%= longitude %></td>
			</tr>
<% 	} %>
	        <tr><td id="count"><%= count  %></td></tr>
		</tbody>
	</table>
	
	<div>
		<input id="showhide" type="button" onclick="showPoints()" value="Hide points"/>
	</div>
	
	<div class="form-div">
		<form id="form" action="addRoutesToDB.jsp" method="post">
			<div>
				<input type="submit" value="Add route"/>
				<input name="routeName" type="text" placeholder="route name"/>
			</div>			
		</form>
		<form id="formDelete" action="deleteRoute.jsp" method="post">
			<input type="submit" value="Delete route"/>
			<input name="routeName" type="text" placeholder="route name"/>
		</form>
							
		<table>
			<thead><tr><td>Route name</td></tr></thead>
			<tbody>		
	<%
	
		rs = stmt.executeQuery("SELECT name FROM Routes;");
		List<String> routeNames = new ArrayList<String>();
		while (rs.next()) {
			String adding = rs.getString("name");
			String added = routeNames.size() > 0 ? routeNames.get(routeNames.size() - 1) : "";
			if (!adding.equals(added))
				routeNames.add(adding);
		}
	
		for (String str : routeNames)
			out.println("<tr><td>" + str + "</td></tr>");
	%>
			</tbody>
		</table>								
	</div>
	</div>
<%
	stmt.close();
	con.close();
%>
</body>
</html>